#include "visbasewidget.h"

#include "resizerect.h"
#include "sagevis.h"

VisBaseWidget::VisBaseWidget( SageVis *svp, QGraphicsItem *parent) :
    QGraphicsWidget(parent)
{
    qDebug() << "launch visBaseWidget" << endl;

    sageVisParent = svp;

    //scene width and scene height are used to scale size of widget
    sceneWidth = scene()->width();
    sceneHeight = scene()->height();

    //default values for perc of scene width and height to size widget
    widthPercOfScene = .9;
    heightPercOfScene = .9;

    setAcceptsHoverEvents(true);

    //NOTE- I have turned resizing off
    //  this is something I will have to consider later
    //  too many problems in implementation, plus it may only come into play in SAGENext
    resizeRect = new ResizeRect( this );
    resizeRect->setPos( boundingRect().x()+boundingRect().width(), boundingRect().y()+boundingRect().height());
    resizeRect->setZValue(10.0);
    resizeRect->setVisible( false );
}

VisBaseWidget::~VisBaseWidget()
{
    if( resizeRect )
        delete resizeRect;
}

//all vis widgets should have the option to load data from file
//this is a virtual function, so each widget must implement it to fit the data it expects to receive
//   though possibly this data will come from another widget- which I may have to consider later
bool VisBaseWidget::loadData(QString pathToData)
{
    //qDebug() << "visbasewidget load data " << pathToData << endl;

    return true;
}

//bool VisBaseWidget::loadData(QString f1, QString f2)
//{
//    return true;
//}

//RESIZE
//won't be called for now- too many implementation issues
//not an immediate priority
void VisBaseWidget::resize(qreal w, qreal h)
{
    qDebug() << "reimplemented resize " << endl;

    widthPercOfScene = w / sceneWidth;
    heightPercOfScene = h / sceneHeight;
}

void VisBaseWidget::advance()
{

}

//------------------------DEFAULT DRAWING APPROACHES------------------------------
//will frequently be overwritten in derived classes
//note- all graphics items need to implement these in order to be drawn in the scene
QRectF VisBaseWidget::boundingRect() const
{
    //return QRectF(-512, -316, 1024, 632);
    //scene width and scene height are used to scale size of widget
//    sceneWidth = scene()->width();
//    sceneHeight = scene()->height();
    return QRectF( -scene()->width()/2.0*widthPercOfScene, -scene()->height()/2.0*heightPercOfScene, scene()->width()*widthPercOfScene, scene()->height()*heightPercOfScene );
                //-sceneWidth/2.0*widthPercOfScene, -sceneHeight/2.0*heightPercOfScene, sceneWidth*widthPercOfScene, sceneHeight*heightPercOfScene );
}

QPainterPath VisBaseWidget::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}


void VisBaseWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QColor background(25, 25, 25, 255 );
    painter->setBrush(background);

    QRectF bRect = boundingRect();
    bRect.adjust(1, 1, -1, -1);
    painter->drawRect(bRect);
}
